#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>

//Display an array
  void printmas (int *A, int N)
 {
      int i;
      for (i=0; i<N; i++)
      printf ("%d ", A[i]);
 }

//Fill an array of random numbers
 void fillrand (int *A, int N)
 {
      int i;
      for (i=0; i<N; i++)
      A[i] = rand()%10;
 }

//Sort by insert
void Insert_Sort(int *A, int N)
{
  int i, j, temp, c=0, m=0;
  for (i=1; i<N; i++)
  {
    for (j=i-1; j>=0; j--)
    {
      c++;
      if (A[j]>A[j+1])
      {
        temp=A[j+1];
        A[j+1]=A[j];
        A[j]=temp;
         m+=3;
      }
      else break;


    }
  }
 printf("\n");
 printmas(A,N);
 printf ("\n Moves = %d", m);
 printf ("\n Compares = %d ", c);
}


int main()
{
    int N=10, A[N], C, M;
    srand(time(NULL));
    printf ("\n \n Random array for Insert Sort:\n");
    fillrand(A,N);
    printmas(A,N);
    printf("\n");
    Insert_Sort(A,N);
    printf("\n");
    C=(N*(N-1))/2;
    M=(3*(N*N-N))/2;
    printf("C=%d \t M=%d \n", C,M);
    system ("PAUSE");
    return 0;
}
